package tasks.leetcode;

public class TaskMultiply {
    public static void main(String[] args) {
        String multiply = new TaskMultiply().multiply("123", "12");
        System.out.println("multiply:" + multiply);
    }

    // 123 * 12
    public String multiply(String num1, String num2) {
        if (num1.equals("0") || num2.equals("0")) {
            return "0";
        }
        int left = num1.length();
        int right = num2.length();
        int[] array = new int[left + right];
        for (int i = left - 1; i >= 0; i--) {
            int c1 = num1.charAt(i) - '0';
            for (int j = right - 1; j >= 0; j--) {
                int c2 = num2.charAt(j) - '0';
                int sum = c1 * c2 + array[i + j + 1];
                array[i + j + 1] = sum % 10;
                array[i + j] += sum / 10;
            }
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            if (array[i] == 0 && i == 0) {
                continue;
            }
            stringBuilder.append(array[i]);
        }
        return stringBuilder.toString();
    }
}
